package cst.a112gxl.TeacherPlatform112gxl.model;



import java.util.List;

import cst.a112gxl.TeacherPlatform112gxl.bean.ArticleBean;
import cst.a112gxl.TeacherPlatform112gxl.bean.CollectBean;
import cst.a112gxl.TeacherPlatform112gxl.bean.LoginBean;
import cst.a112gxl.TeacherPlatform112gxl.iface.AmicoolIface;
import cst.a112gxl.TeacherPlatform112gxl.listener.AmicoolListener;
import cst.a112gxl.TeacherPlatform112gxl.service.ResService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by Administrator on 2017/6/3/003.
 */

public class Resmodel implements AmicoolIface {
    private Retrofit retrofit;
    private ResService service;
    private static final String  BASE_URL = "http://amicool.neusoft.edu.cn/";

    public Resmodel(String type) {
        if (type.equals("gson")){
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }else if (type.equals("string")){
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
            service = retrofit.create(ResService.class);
    }

    @Override
    public void login(String username, String password,  final AmicoolListener l) {
        Call<LoginBean> call = service.login(username,password);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    l.onResponse(response.body());
                }
                else
                    l.onFail("ԃۧһզ՚");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void reg(String username, String password, String tel, final AmicoolListener l) {
        Call<String> call = service.reg(username,password,tel);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    l.onResponse(response.body());
                }
                else
                    l.onFail("עӡʧќ");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getList(final String mod, int page, String session, final AmicoolListener l) {
        Call<List<ArticleBean>> call = service.getArticleList(mod,page,session);
        switch (mod){
            case "article":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            l.onResponse(response.body());
                        }
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "video":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tcase":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "project":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "video2":
                Call<List<ArticleBean>> videocall = service.getvideo2("video",page,session);
                videocall.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            l.onResponse(response.body());
                        }
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
        }


    }

    @Override
    public void getDetail(String mod, int id,  final AmicoolListener l) {
        Call<ArticleBean> call = service.getDetail(mod, id);
        call.enqueue(new Callback<ArticleBean>() {
            @Override
            public void onResponse(Call<ArticleBean> call, Response<ArticleBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ArticleBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });

    }

    @Override
    public void iscollect(String mod, int resid, String session, final AmicoolListener l) {
        Call<String> call = service.cexists(mod, resid, session);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void collect(String mod, int resid, String session,final AmicoolListener l) {
        Call<String> call = service.collect(mod, resid, session);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void uncollect(String mod, int resid, String session,final AmicoolListener l) {
        Call<String> call = service.uncollect(mod, resid, session);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getResultList(String mod, int page, String session, final AmicoolListener l) {
        Call<List<CollectBean<ArticleBean>>> call = service.getCArticleList(mod,page,session);
        call.enqueue(new Callback<List<CollectBean<ArticleBean>>>() {
            @Override
            public void onResponse(Call<List<CollectBean<ArticleBean>>> call, Response<List<CollectBean<ArticleBean>>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<CollectBean<ArticleBean>>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }


    @Override
    public void isfocus(String mod, int idolid, String session,final AmicoolListener l) {
        Call<String> call = service.fexists(mod, idolid, session);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void focus(String mod, int idolid, String session,final AmicoolListener l) {
        Call<String> call = service.focus(mod, idolid, session);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void unfoucs(String mod, int idolid, String session,final AmicoolListener l) {
        Call<String> call = service.unfocus(mod, idolid, session);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getCollectList(String mod, int page, String sessionID, final AmicoolListener l) {
        Call<List<CollectBean<ArticleBean>>> call =service.getCArticleList(mod,page,sessionID);
        call.enqueue(new Callback<List<CollectBean<ArticleBean>>>() {
            @Override
            public void onResponse(Call<List<CollectBean<ArticleBean>>> call, Response<List<CollectBean<ArticleBean>>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<CollectBean<ArticleBean>>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getFocustList(String mod, int page, String sessionID,final AmicoolListener l) {
        Call<List<CollectBean<LoginBean>>> call =service.getFocusUserList(mod,page,sessionID);
        call.enqueue(new Callback<List<CollectBean<LoginBean>>>() {
            @Override
            public void onResponse(Call<List<CollectBean<LoginBean>>> call, Response<List<CollectBean<LoginBean>>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<CollectBean<LoginBean>>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }


    @Override
    public void getFocusArticletList(String mod, int page, String sessionID, int userid, final AmicoolListener l) {
        Call<List<ArticleBean>> call = service.getArticleList(mod, page, sessionID, userid);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });

    }
}